package com.liruo.treasureattic.common.util;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @Author:liruo
 * @Date:2023-02-07-18:08:32
 * @Desc  目的是为了可以套娃
 * 时区问题:  使用默认时区的用默认时区转换
 *     仅Instant和Date是保持UTC时间(但Date在输出时转为系统时区)
 *     LocalDateTime是默认时区，ZonedDateTime使用LocalDateTime保持时间
 */
public class DefaultTimeUtils {

    private final static DateTimeFormatter defaultDateTimeFormatter =
            DateTimeFormatter
                    .ofPattern("yyyy-MM-dd HH:mm:ss")
                    .withZone(ZoneId.systemDefault());

    public static ZoneOffset ZoneIdToOffset(ZoneId zoneId){
        return zoneId.getRules().getOffset(Instant.now());
    }

    //parse
    public static LocalDateTime parse(String timeStr) {
        return LocalDateTime.parse(timeStr, defaultDateTimeFormatter);
    }
    public static Date parseToDate(String timeStr) {
        return toDate(LocalDateTime.parse(timeStr, defaultDateTimeFormatter));
    }
    public static Instant parseToInstant(String timeStr) {
        return toInstant(LocalDateTime.parse(timeStr, defaultDateTimeFormatter));
    }
    public static Instant parseMilli(String timestampMilli) {
        return Instant.ofEpochMilli(Long.parseLong(timestampMilli));
    }
    public static Instant parseSecond(String timestampSecond) {
        return Instant.ofEpochSecond(Long.parseLong(timestampSecond));
    }


    //format

    private String a;
    public static String format(Date date) {
        return format(toLocalDateTime(date));
    }
    public static String format(Instant instant) {
        return format(toLocalDateTime(instant));
    }
    public static String format(LocalDateTime localDateTime) {
        return localDateTime.format(defaultDateTimeFormatter);
    }

    /**
     * @param timestamp ofMilli
     */
    public static String format(long timestamp) {
        return format(ofMilli(timestamp));
    }
    /**
     * @param timestamp ofSecond
     */
    public static String formatS(long timestamp) {
        return format(ofSecond(timestamp));
    }

    //timestamp to Instant
    public static Instant ofMilli(long timestamp) {
        return Instant.ofEpochMilli(timestamp);
    }
    public static Instant ofSecond(long timestamp) {
        return Instant.ofEpochSecond(timestamp);
    }

    //date to Instant  Second-getEpochSecond Millis-toEpochMilli

    public static Instant toInstant(LocalDateTime localDateTime) {
        return localDateTime.atZone(ZoneId.systemDefault()).toInstant();
    }
    public static Instant toInstant(Date date) {
        return date.toInstant();
    }

    //Instant to date

    public static LocalDateTime toLocalDateTime(Instant instant) {
        return  LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
    }
    public static Date toDate(Instant instant) {
        return Date.from(instant);
    }


    //转换

    public static LocalDateTime toLocalDateTime(Date date) {
        return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
    }
    public static Date toDate(LocalDateTime localDateTime) {
        return Date.from(toInstant(localDateTime));
    }
}
